# include "ObjectPickUp.h"

# include "Renderer.h"

ObjectPickUp::ObjectPickUp()
{

}

ObjectPickUp::~ObjectPickUp()
{

}

void ObjectPickUp::setPosition( const Ogre::Vector3& position)
{
	mySceneNode->setPosition( position);
}

unsigned int ObjectPickUp::getObjectType()
{
	return myObjectType;
}

const std::string& ObjectPickUp::getName()
{
	return myName;
}

void ObjectPickUp::init( Renderer* renderer)
{
	myRenderer = renderer;
}

void ObjectPickUp::load( const std::string& name, unsigned int indice)
{
	myType = OBJECT;

	lua_State* stateTmp = lua_open();
	luaL_openlibs( stateTmp);
	luaL_dofile( stateTmp, LUA_OBJECT_ACTION);
	lua_settop( stateTmp, 0);
	unsigned int indiceTmp = 1;

	lua_getglobal( stateTmp, name.c_str());

	std::ostringstream streamTmp;
	streamTmp << name << indice;
	
	lua_pushnumber( stateTmp, indiceTmp++);
	lua_gettable( stateTmp, -2);
	myEntity = myRenderer->createEntity( streamTmp.str(), lua_tostring( stateTmp, -1));
	lua_pop( stateTmp, 1);

	streamTmp << "Node";

	mySceneNode = myRenderer->createSceneNode( streamTmp.str());
	myEntity->setQueryFlags( ACTION_FLAG);
	mySceneNode->attachObject( myEntity);
	
	lua_pushnumber( stateTmp, indiceTmp++);
	lua_gettable( stateTmp, -2);
	myObjectType = lua_tonumber( stateTmp, -1);
	lua_pop( stateTmp, 1); 

	lua_pushnumber( stateTmp, indiceTmp++);
	lua_gettable( stateTmp, -2);
	myName = lua_tostring( stateTmp, -1);
	lua_pop( stateTmp, 1);

	lua_pop( stateTmp, 1);
	lua_close( stateTmp);
}